package com.ht.medicaltreatment.view

import android.R
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class XItemDecoration(context: Context,orientation:Int,drawable:Int,inset:Int = 15) : RecyclerView.ItemDecoration() {
    private val ATTRS = intArrayOf(R.attr.listDivider)
    var mDivider:Drawable
    private val HORIZONTAL = LinearLayoutManager.HORIZONTAL
    private val VERTICAL = LinearLayoutManager.VERTICAL
    private val mOrientation = orientation
    //分割线缩进值
    private var inset = inset
    private var paint:Paint
    init {
        mDivider = context.getDrawable(drawable)!!
        paint = Paint()
        //偏移量下面的背景色
        paint.setColor(context.getColor(R.color.white));
        paint.setStyle(Paint.Style.FILL)
        paint.setAntiAlias(true)
    }

    //画分割线
    override fun onDraw(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        if (mOrientation == VERTICAL) {
            drawVertical(canvas, parent);
        } else {
            drawHorizontal(canvas, parent);
        }
    }

    private fun drawHorizontal(canvas: Canvas, parent: RecyclerView) {


    }

    private fun drawVertical(canvas: Canvas, parent: RecyclerView) {
        val left = parent.paddingLeft
        val right = parent.width - parent.paddingRight

        for (i in 0 until parent.childCount){
            val childView = parent.getChildAt(i)
            val param = childView.layoutParams as RecyclerView.LayoutParams
            val top = childView.bottom + param.bottomMargin
            val bottom = top + mDivider.intrinsicHeight
            canvas.drawRect(left.toFloat(), top.toFloat(), right.toFloat(),bottom.toFloat(),paint)
            mDivider.setBounds(left+inset, top, right-inset,bottom)
            mDivider.draw(canvas)
        }
    }

    override fun getItemOffsets(outRect: Rect,view: View,parent: RecyclerView,state: RecyclerView.State) {
        if (mOrientation == VERTICAL) {
            outRect.set(0, 0, 0, mDivider.getIntrinsicHeight())
        } else {
           // outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0)
        }
    }
}